#! /bin/bash
source .env
source ../common.sh

LINE_STR="==============================>>>"
echo "${LINE_STR} commands available to users: "
echo "bash start.sh: 启动容器"
echo "bash start.sh start: 启动容器"
echo "bash start.sh save: 保存镜像"
echo "bash start.sh load: 加载镜像"
echo "bash start.sh config: 打印配置"

function start() {
  docker-compose down
  docker-compose up -d
}

# 保存镜像
function imageSave() {
  echo "${LINE_STR} before save image"
  if [ -f "$PWD/redis.tar.gzl" ]; then
    rm -rf redis.tar.gz
  fi
  docker save redis:6.2.7 | gzip > redis.tar.gz
  echo "${LINE_STR} after save image"
}

# 加载镜像
function imageLoad() {
  echo "${LINE_STR} before load image"
  gunzip -c redis.tar.gz | docker load
  echo "${LINE_STR} after load image"
}

echo "input command: $1"
echo "image tag:     redis:6.2.7"
if [ "$1" == "start" ]; then
  start 
elif [ "$1" == "save" ]; then
  imageSave
elif [ "$1" == "load" ]; then
  imageLoad
elif [ "$1" == "config" ]; then
  docker-compose config
else
  start
fi